package club.jdiy.core.base;


import java.beans.PropertyEditorSupport;
import java.util.Objects;

/**
 * for:springMVC Number类型自动映射转换识别
 *
 * @author ziquee(子秋) QQ号:39886616 主页: http://www.jdiy.club
 * @version 20200520
 */
public class NumberEditor extends PropertyEditorSupport {
    public enum Type {
        Double, Long, Integer
    }

    public NumberEditor(Type t) {
        Objects.requireNonNull(t, "无效Type类型绑定");
        this.t = t;
    }

    /**
     * text是表单传入的数据内容
     */
    @Override
    public void setAsText(String text) {
        if (text == null || "".equals(text.trim()) || "null".equals(text.trim())) return;
        if (Type.Double.equals(t)) setValue(Double.parseDouble(text));
        else if (Type.Long.equals(t)) setValue(Long.parseLong(text));
        else if (Type.Integer.equals(t)) setValue(Integer.parseInt(text));
    }

    private final Type t;
}